package cn.android.support.v7.lib.sin.crown.builder;

import android.app.Activity;
import android.util.Log;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import cn.android.support.v7.lib.sin.crown.utils.HttpUtils;


/**
 * Build模式封装网络请求，支持Get请求，POST请求，和POST图片上传。
 * Created by 彭治铭 on 2018/1/9.
 */

public class HttpBuilder {
    private static HttpBuilder httpBuilder;

    //url是基础，buider的时候，必须传人。
    //url和回调函数没有set方法。
    public static HttpBuilder Builder(String url) {
        if (httpBuilder == null) {
            httpBuilder = new HttpBuilder();
        }
        //清空参数，不用担心之前请求的参数为空，因为UtilHttp里的传参都是final类型。
        httpBuilder.url = url;
        httpBuilder.methodPost = true;
        httpBuilder.body = null;
        httpBuilder.header = null;
        httpBuilder.params = null;
        httpBuilder.files = null;

        return httpBuilder;
    }

    private HttpBuilder() {
    }

    /**
     * 是否打印params参数
     *
     * @param showParams
     * @return
     */
    public HttpBuilder setShowParams(boolean showParams) {
        isShowParams = showParams;
        return this;
    }

    /**
     * 执行网络请求。如果是Https做了安全验证。访问不了。就把https后的s去掉。改成Http即可访问。
     *
     * @param activity        可以为空(null)
     * @param requestCallBack 回调函数
     */
    public void execute(Activity activity, HttpUtils.RequestCallBack requestCallBack) {

        if (isShowParams&&params!=null) {
            Log.e("test","url链接:\t"+url);
            Iterator it = params.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry entry = (Map.Entry) it.next();
                String key = entry.getKey().toString();
                String value = entry.getValue().toString();
                Log.e("test", "键:\t" + key + "\t值:\t" + value);//打印params提交参数。
            }
        }

//        //为了安全性，header头部添加指定参数
//        addHeader("session", SharePrefManager.getId());
//        //======================================
//        //设置常用的基本参数
//        if (body == null) {//params和files为空时，body才有效，判断body是否空，防止body失效。
//            if (params == null) {
//                params = new HashMap<>();
//            }
//            if (!params.containsKey("id")) {//防止重复覆盖
//                addParam("id", SharePrefManager.getId());
//            }
//        }
//        //======================================

        if (files != null && files.size() > 0) {
            //包含文件上传【文件上传必须为Post请求。】
            HttpUtils.getInstance().sendPostFile(activity, url, header, params, files, requestCallBack);
        } else {
            //普通POST请求。
            if (methodPost) {
                HttpUtils.getInstance().sendPost(activity, url, header, params, body, requestCallBack);
            } else {
                //Get请求。
                HttpUtils.getInstance().sendGet(activity, url, header, params, requestCallBack);
            }
        }
    }

    private String url;//网络请求链接
    private boolean methodPost = true;//是否为POST请求。默认是。false为Get请求。
    private Map<String, String> header;//头部请求信息参数
    private Map<String, String> params;//params请求参数，其实在body里面，属性body子集
    private boolean isShowParams = false;//是否打印params参数,默认不打印
    private Map<String, File> files;//上传的文件集合
    private String body;//body体，param和files为null时，才有效。

    //合并Map
    public Map<String, String> sumMap(Map<String, String> map, Map<String, String> map2) {
        Iterator iter = map.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            Object key = entry.getKey();
            Object val = entry.getValue();
            if (key != null && val != null) {
                map2.put((String) key, (String) val);
            }
        }
        return map2;
    }

    //合并Map
    public Map<String, File> sumMap2(Map<String, File> map, Map<String, File> map2) {
        Iterator iter = map.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            Object key = entry.getKey();
            Object val = entry.getValue();
            if (key != null && val != null) {
                map2.put((String) key, (File) val);
            }
        }
        return map2;
    }

    /**
     * 设置头部
     *
     * @param header
     * @return
     */
    public HttpBuilder setHeader(Map<String, String> header) {
        if (this.header == null) {
            this.header = header;
        } else {
            this.header = sumMap(this.header, header);
        }
        return this;
    }

    /**
     * 添加头部
     *
     * @param key
     * @param val
     * @return
     */
    public HttpBuilder addHeader(String key, String val) {
        if (this.header == null) {
            this.header = new HashMap<>();
        }
        header.put(key, val);
        return this;
    }

    /**
     * 设置params 参数。一般都是这个。params在body里面
     *
     * @param params
     * @return
     */
    public HttpBuilder setParams(Map<String, String> params) {
        if (this.params == null) {
            this.params = params;
        } else {
            this.params = sumMap(this.params, params);
        }
        return this;
    }

    /**
     * 添加Params
     *
     * @param key
     * @param val
     * @return
     */
    public HttpBuilder addParam(String key, String val) {
        if (this.params == null) {
            this.params = new HashMap<>();
        }
        params.put(key, val);
        return this;
    }

    /**
     * 上传文件【如果有文件上传，直接使用POST请求】
     *
     * @param files
     * @return
     */
    public HttpBuilder setFiles(Map<String, File> files) {
        if (this.files == null) {
            this.files = files;
        } else {
            this.files = sumMap2(this.files, files);
        }
        return this;
    }

    /**
     * 添加文件Fi
     *
     * @param key
     * @param file
     * @return
     */
    public HttpBuilder addFile(String key, File file) {
        if (this.files == null) {
            this.files = new HashMap<>();
        }
        files.put(key, file);
        return this;
    }

    public HttpBuilder setBody(String body) {
        this.body = body;
        return this;
    }

    /**
     * @param methodPost true post请求【默认】，false get请求。
     * @return
     */
    public HttpBuilder setMethodPost(boolean methodPost) {
        this.methodPost = methodPost;
        return this;
    }
}
